<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<style type="text/css">
	body {
		background: #404066;
		color: #000000;
		font: 10pt verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif;
		margin: 5px 10px 10px 10px;
		padding: 0px;
		overflow: hidden;
	}
	h1 {
		color: darkblue;
		font :bold 16pt verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;
		margin-top: 0
	}
	.title {
		text-align: center;
	}
	h2 {
		color:darkblue;
		font:bold 14pt verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif
	}
	h3 {
		color:darkblue;
		font:bold 12pt verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif
	}
	h4 {
		color:#b73235;
		font:bold 10pt verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif
	}
	pre {
		font: 10pt parallax,courier,monospaced;
		line-height: 97%;
	}	
	.source {
		margin-left: 20px;
		border: 1px solid #ccc;
		overflow: auto;
		display: none;
		background: #f8f8e8;
		margin-top: 12pt;
		width: 95%;
		padding-top: 5px;
		padding-left: 10px;
		max-height: 600px;
		letter-spacing: 0px;
	}
	var {
		color: #000080;
		font-style: normal;
		font-weight: bold;
		letter-spacing: -1px;
	}
	a {
		font-weight: bold;
		font-size: 6pt;
		text-decoration: none;
		color: #0000cc;
	}
	.toc1 {
		font-size: 12pt;
		font-weight: bold;
		vertical-align: baseline;
		text-align: center;
		color: #d7d0ff;
	}		
	.toc2 {
		font-size: 95%;
		font-weight: bold;
		vertical-align: baseline;
		color: white;
		text-decoration: none;
	}		
	.toc3 {
		font-size: 90%;
		font-weight: normal;
		vertical-align: baseline;
		margin-left: 10px;
		color: white;
		text-decoration: none;
	}		
	.toc4 {
		font-size: 85%;
		font-weight: normal;
		font-style: italic;
		vertical-align: baseline;
		margin-left: 20px;
		color: #d7d0ff;
		text-decoration: none;
	}
	.tocgrp {
		display: none
	}
  hr {
  	border: 0;
    color: #b73235;
    background-color: #b73235;
    height: 1px;
  }
	</style>
	<script language="JavaScript">
		window.onload = resize;
		window.onresize = resize;
		var show=0;
		function ShowHide(divId) {
			if (document.getElementById(divId).style.display!='block') {
				document.getElementById(divId).style.display='block';
				if (divId.substring(0, 3) == 'toc') {
					window.location = '#lbl' + divId.substring(3);
				}
			} else {
				document.getElementById(divId).style.display='none';
			}
		};
    function resize() {
      var myWidth = 0, myHeight = 0;
      if(typeof(window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
      } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
      } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
      }
      document.getElementById("text").style.height = (myHeight - 10) + "px";
      document.getElementById("text").style.width = (Math.min(myWidth - 250, 800)) + "px";
      document.getElementById("toc").style.height = (myHeight - 10) + "px";
     }	
    function selectall(source) { 
	    var selection, range, doc, win; 
	    if ( (doc = source.ownerDocument)
	    	&& (win = doc.defaultView)
	    	&& typeof win.getSelection != 'undefined'
	    	&& typeof doc.createRange != 'undefined' 
	    	&& (selection = window.getSelection())
	    	&& typeof selection.removeAllRanges != 'undefined'
	    ) { 
	       range = doc.createRange(); 
	       range.selectNode(source); 
	       selection.removeAllRanges(); 
	       selection.addRange(range); 
	    } else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) { 
	       range.moveToElementText(source); 
	       range.select(); 
	    } 
		}
  </script>
</head>
<body style = "margin: 0 0 0 0;">
<div align=center>
<table cellpadding=0 cols=2 border=0>
	<tr><td valign="top">
	<div id="toc" style= "width: 200px; height: 600px; overflow: auto; background-color: #222255; border-left: solid #ffffe0"><br/>
		<div style="background-color:#222255; width: 100%; text-align: center">
		<img src="data:image/gif;base64,
			R0lGODlhrwBFALMAABAIPyIiVRwQYDUrcjQmSkwnSk9OW2wrQp1PYKgxMU1KiKCeqvv7++Tk58rJ
			0G5niiwAAAAArwBFAAAI/gADCBxIsKDBgwgTKlzIsKHDhxAjSpxIsaLFixgzatzIsaPHjyAfEhhJ
			cmSBkyhTqlzJsqXLlzBjypxJs2bJkRhL1tzJs6fPn0BhlqyoM6jRo0iTAiUpsajKA1CjSp1KtarV
			q1izat3KtatXqCuZOnSK0mqCs2jTql3Ltq3bt3Djyp1L123VlDgbknwqta7fv4ADCx5sF2XehXtT
			Rl2LoLHjx5AjS55MubLly5gza97c+OwBwwQQmywLNW3jB6hTq17NurXr17Bjy55Nu7Zt1QgSfD55
			GOHok4vPIkitoLjx48iTK1/OvLnz59CjS59u/EHu3bwV/i4QfPgD4wPC/osfT768+fPo06tfz769
			+/fhFVjXjT2079+lhX9XMF6A//8ABijggAQWaOCBCCao4IIMxnddfQnhl99w/IXH4IUYZqjhhhwK
			6CB9ht1H2gH6VdjhiSimqGKDAyiQm24hHiRhiRauaOONOHb4IYgF2FfQjAlQaCEARBZppJEKHqkk
			kggCAKCTOf4HJZQMUhngklYKsCNYPcpIAGlnfWchAQaUaeaZaGZ5JZlotmmmmk+SGYB/ABhAQJQA
			yDkSnAUGYKeAfrppwJz+bbmbjwMBKeYAAhCwAAOQRiqppA08wGedDkyqaaSVXupoAwYQaYADC/DZ
			YZ4LgLqAA6Em+UAD/6U+aUADm7IKpaHZ/fglcCQKOUCemTYg7LDEDstApwJiCmmxzAp7rKXJEpAp
			A5YC8MCxpm6IKqQPPApqtnS+ykCsUo7qwLnoktqqli2+yCWiAo02oYnAUivomcFCW+60495rprf6
			0iltpNVe2wC4GNbL7cDfGmgtrQvceWWeNxFA5H+4dvkjmAmIKXCmoWJZ5KgMOGDxx8dyK3KRjpZ8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pjng1DhqO
			Y5fKoqaO8pfP/OY7f/mpeVHSNm587BZAOJzJvva3z/3ugntf+sQvPkPwQ5jym//86A+MVPCS9Pt8
			Mzjpj7/8518Xqqgk7qJh2oi+wv/++///APh/YYF/45cYKxGACJiACriAd+ESBDgWBqgUEjiBFHgU
			N0EUZFGBGriBHFh8DzgRcRWCIjiCJFiCJniCKJiCKoiCIdGCLviCMBiDMjiDNAiDAQEAOw==
		">
		</div>
		<div style="font: bold 11pt verdana,geneva,lucida,'lucida grande',arial,helvetica,sans-serif;color:white; text-align: center; margin-bottom: 10px; margin-top: 10px">DNS</div>
		<p class = "toc1">Table of Contents</p>
		<div style = "text-align: left; margin-left: 10px;">
			<a href= "#lbl1" class = "toc2">Preface</a><br/>
<a href= "#lbl2" class = "toc2">Global CONstants </a><br/>
<a href= "#lbl5" class = "toc2">Global DATa </a><br/>
<a href= "#lbl7" class = "toc2">Used OBJects </a><br/>
<a href= "#lbl9" class = "toc2">PUBlic Spin Methods</a><br/>
<a href= "#lbl10" class = "toc4">Init</a><br/>
<a href= "#lbl12" class = "toc4">SetDnsServerIp</a><br/>
<a href= "#lbl14" class = "toc4">GetIpCount</a><br/>
<a href= "#lbl16" class = "toc4">GetResolvedIp</a><br/>
<a href= "#lbl18" class = "toc4">IsNullIp</a><br/>
<a href= "#lbl20" class = "toc4">ResolveDomain</a><br/>
<a href= "#lbl22" class = "toc4">ParseUrl</a><br/>
<a href= "#lbl24" class = "toc4">GetRcode</a><br/>
<a href= "#lbl26" class = "toc4">RCodeError</a><br/>
<a href= "#lbl28" class = "toc4">ParseDnsResponse</a><br/>
<a href= "#lbl30" class = "toc4">CreateTransactionId</a><br/>
<a href= "#lbl32" class = "toc4">FillTransactionID</a><br/>
<a href= "#lbl34" class = "toc4">SendReceive</a><br/>
<a href= "#lbl36" class = "toc2">Documentation </a><br/>
<a href= "#lbl37" class = "toc2">MIT License </a><br/>

		</div>
	</div>
	</td>
	<td>
		<div id="text" style= "text-align: left; padding-left: 20px; padding-right: 20px; width: 800px; height: 600px; background-color: #ffffe0; overflow: auto">
			<h1 id = "lbl1" class = "title">DNS </h1>

<br /><br />
AUTHOR:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mike Gebhard<br />
COPYRIGHT:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parallax Inc.<br />
LAST MODIFIED:&nbsp;&nbsp;&nbsp;&nbsp;10/19/2013<br />
VERSION:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.0<br />
LICENSE:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIT (see end of file)<br /><br />
DESCRIPTION:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The DNS object<br />
<br />
MODIFICATIONS:<br />
10/05/2013&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;added minimal spindoc comments<br />
10/19/2013&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;moved SendReceive to socket<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Michael Sommer (MSrobots)<br />

<br />
<h2 id = "lbl2">Global CONstants </h2>
<br /><a onclick ="javascript:ShowHide('lbl3')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl3>
  BUFFER_2K         = $800
  BUFFER_16         = $10
  URL_BUFFER        = $80
  
  CR                = $0D
  LF                = $0A
  DOT               = $2E 

  #0, CLOSED, TCP, UDP, IPRAW, MACRAW, PPPOE

</pre>
<hr /> DNS Packet Enum<br />
<br /><a onclick ="javascript:ShowHide('lbl4')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl4>  TRANSACTION       = $00
  FLAGS             = $02
  QUESTIONS         = $04
  ANSWERS           = $06
  AUTHORITY         = $08
  ADDITIONAL        = $0A
  QUERY             = $0C
  DNS_HEADER_LEN    = QUERY

  DNS_PORT          = 53               

</pre>
<hr />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<h2 id = "lbl5">Global DATa </h2>
<br /><a onclick ="javascript:ShowHide('lbl6')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl6>
  msgId           <var>word</var>  $0
  dnsHeader       <var>byte</var>  $01, $00,               { Flags
}                       $00, $01,               { Questions
}                       $00, $00,               { Answer RRS
}                       $00, $00,               { Authority RRS
}                       $00, $00                { Additional RRS }

  qtype           <var>byte</var>  $00, $01                { Host address: Type A }
  qclass          <var>byte</var>  $00, $01                { Class: 01 }

  urlBuff         <var>byte</var>  $0[URL_BUFFER]              
  rc0             <var>byte</var>  "No error condition." ,$0
  rc1             <var>byte</var>  "Format error", $0
  rc2             <var>byte</var>  "Server failure", $0
  rc3             <var>byte</var>  "Name Error", $0
  rc4             <var>byte</var>  "<var>Not</var> Implemented", $0
  rc5             <var>byte</var>  "Refused", $0
  rc6             <var>byte</var>  "Unknow error", $0
  rc7             <var>byte</var>  "DNS IP is empty", 0
  rcPtr           <var>long</var>  @rc0, @rc1, @rc2, @rc3, @rc4, @rc5, @rc6, @rc7
  rcode           <var>byte</var>  $0

  dnsServerIp     <var>byte</var>  $00, $00, $00, $00      '68, 105, 28, 12

  ip1             <var>byte</var>  $00, $00, $00, $00
  ip2             <var>byte</var>  $00, $00, $00, $00
  ip3             <var>byte</var>  $00, $00, $00, $00
  ip4             <var>byte</var>  $00, $00, $00, $00
  ip5             <var>byte</var>  $00, $00, $00, $00
  ip6             <var>byte</var>  $00, $00, $00, $00
  ip7             <var>byte</var>  $00, $00, $00, $00
  ip8             <var>byte</var>  $00, $00, $00, $00
  ip9             <var>byte</var>  $00, $00, $00, $00
  ip10            <var>byte</var>  $00, $00, $00, $00
  ip11            <var>byte</var>  $00, $00, $00, $00
  ip12            <var>byte</var>  $00, $00, $00, $00
  dnsCnt          <var>byte</var>  $00
  dnsIps          <var>long</var>  @ip1, @ip2, @ip3, @ip4, @ip5, @ip6, @ip7, @ip8, @ip9, @ip10, @ip11, @ip12

  buffPtr         <var>long</var>  $00
  transId         <var>long</var>  $00
  null            <var>long</var>  $00

</pre>
<hr /><br />
<h2 id = "lbl7">Used OBJects </h2>
<br /><a onclick ="javascript:ShowHide('lbl8')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl8>
  sock          : "Socket"
  wiz           : "W5100"

</pre>
<hr /><br />
<h2 id = "lbl9">PUBlic Spin Methods</h2>
<hr /><h4 id = "lbl10">Init(buffer, socket)
</h4>

DESCRIPTION:

PARMS:
  
RETURNS:
  
<br />
<br /><a onclick ="javascript:ShowHide('lbl11')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl11><var>PUB</var> Init(buffer, socket) | dnsPtr
  buffPtr := buffer

  'DNS Port, Mac and Ip 
  sock.Init(socket, UDP, DNS_PORT)

  'Get the default DNS from DHCP
  'dnsPtr := wiz.GetRouter
  dnsPtr := wiz.GetDns

  'Note: The DNS IP could be null if DHCP is not used
  'or DNS is not manully set. 
  <var>if</var>(dnsPtr &gt; NULL) 
    sock.RemoteIp(<var>byte</var>[dnsPtr][0], <var>byte</var>[dnsPtr][1], <var>byte</var>[dnsPtr][2], <var>byte</var>[dnsPtr][3])
    sock.RemotePort(DNS_PORT)
    <var>return</var> <var>true</var>
  <var>else</var>
    rcode := 7
    <var>return</var> <var>false</var>
    
'Use this if you need to manually set DNS

</pre>
<hr /><h4 id = "lbl12">SetDnsServerIp(octet3, octet2, octet1, octet0)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl13')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl13><var>PUB</var> SetDnsServerIp(octet3, octet2, octet1, octet0)
  dnsServerIp[0] := octet3
  dnsServerIp[1] := octet2
  dnsServerIp[2] := octet1
  dnsServerIp[3] := octet0
  wiz.CopyDns(@dnsServerIp, 4)
  sock.RemoteIp(octet3 , octet2, octet1, octet0)
  sock.RemotePort(53)

</pre>
<hr /><h4 id = "lbl14">GetIpCount
</h4>
<br /><a onclick ="javascript:ShowHide('lbl15')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl15><var>PUB</var> GetIpCount
  <var>return</var> dnsCnt

</pre>
<hr /><h4 id = "lbl16">GetResolvedIp(idx)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl17')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl17><var>PUB</var> GetResolvedIp(idx)

  <var>if</var>(idx &gt; dnsCnt-1)
    <var>return</var> @null
    
  <var>if</var>(IsNullIp( @dnsIps[idx] ) )
    <var>return</var> @null
    
  <var>return</var> @@dnsIps[idx]

</pre>
<hr /><h4 id = "lbl18">IsNullIp(ipaddr)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl19')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl19><var>PRI</var> IsNullIp(ipaddr)
  <var>return</var> (<var>byte</var>[ipaddr][0] + <var>byte</var>[ipaddr][1] + <var>byte</var>[ipaddr][2] + <var>byte</var>[ipaddr][3]) == 0 

</pre>
<hr /><h4 id = "lbl20">ResolveDomain(url)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl21')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl21><var>PUB</var> ResolveDomain(url) | ptr, dnsPtr

  <var>bytefill</var>(@ip1, 0, @dnsIps-@ip1)

  <var>bytemove</var>(@urlBuff, url, <var>strsize</var>(url)+1)
  
  {   }  
  dnsPtr := wiz.GetDns
  sock.RemoteIp(<var>byte</var>[dnsPtr][0], <var>byte</var>[dnsPtr][1], <var>byte</var>[dnsPtr][2], <var>byte</var>[dnsPtr][3])
  sock.RemotePort(DNS_PORT)

  CreateTransactionId($FFFF)
  FillTransactionID
  
  'Copy header to the buffer
  <var>bytemove</var>(buffPtr, @msgId, DNS_HEADER_LEN)
  
  'Format and copy the url
  ptr := ParseUrl(@urlBuff, buffPtr+DNS_HEADER_LEN)
  
  'Add the QTYPE and QCLASS
  <var>bytemove</var>(ptr, @QTYPE, 4)
  ptr += 4

  ptr := sock.SendReceive(buffPtr, ptr - buffPtr)

  <var>if</var>(ptr == -1)
    dnsCnt := 0
  <var>else</var>  
    ParseDnsResponse(ptr)
  <var>return</var>  GetResolvedIp(0)
  
  'return  ptr

</pre>
<hr /><h4 id = "lbl22">ParseUrl(src, dest)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl23')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl23><var>PRI</var> ParseUrl(src, dest) | ptr
  ptr := src

  <var>repeat</var> <var>strsize</var>(src)
    <var>if</var> ( <var>byte</var>[ptr++] == DOT )
      <var>byte</var>[ptr-1] := NULL                                'Replace dot with a zero
      <var>byte</var>[dest++] := <var>strsize</var>(src)                       'Insert url segment len
      <var>bytemove</var>(dest, src, <var>strsize</var>(src))                  'Insert url segment
      dest += <var>strsize</var>(src)                               'set pointers
      src := ptr

  <var>byte</var>[dest++] := <var>strsize</var>(src)                           'Insert last url segment
  <var>bytemove</var>(dest, src, <var>strsize</var>(src))
  dest += <var>strsize</var>(src)
  <var>byte</var>[dest++] := NULL
  
  <var>return</var> dest

</pre>
<hr /><h4 id = "lbl24">GetRcode(src)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl25')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl25><var>PUB</var> GetRcode(src)

  CreateTransactionId($FFFF)
  FillTransactionID
  
  rcode := <var>byte</var>[src+FLAGS+1] &amp; $000F
  <var>return</var> rcode

</pre>
<hr /><h4 id = "lbl26">RCodeError
</h4>
<br /><a onclick ="javascript:ShowHide('lbl27')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl27><var>PUB</var> RCodeError
  <var>case</var> rcode
    0..7  : <var>return</var> @@rcPtr[rcode]
    <var>other</var> : <var>return</var> @rc6

</pre>
<hr /><h4 id = "lbl28">ParseDnsResponse(buffer)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl29')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl29><var>PRI</var> ParseDnsResponse(buffer) | i, len, ansRRS

  i := 0
  
  ' The number of answers to expect
  ansRRS := wiz.DeserializeWord(buffer+ANSWERS)

  '--------------------------------------
  'Query Section
  '--------------------------------------
  'Point to the query section which contains 
  'the plain text url to resolve.  Loop until 
  'we reach a zero then jump 4 bytes.
  buffer += QUERY
  <var>repeat</var> <var>until</var> <var>byte</var>[buffer++] == $00
  'Jump past the Type(2) and Class(2)
  buffer += 4

  '-------------------------------------- 
  'Answer Section
  '--------------------------------------
  'The idea is to loop through the answer section
  'and grab the IPs. Nto every answer section will
  'have an IP.  Somethimes they have text only
  <var>repeat</var> ansRRS
    'Encoded name 
    <var>if</var>(<var>byte</var>[buffer] &amp; $C0 == $C0)
      'Check for another encoded name                       
      <var>if</var>(<var>byte</var>[buffer+2] &amp; $C0 == $C0)
        'We have two encoded names to skip                 
        buffer += 2
      'Skip Name(2), Type(2), Class(2), and TTL(4)
      buffer += 10  
    <var>else</var>
      'Increment the pointer until we reach the end of the name
      <var>repeat</var> <var>until</var> <var>byte</var>[buffer++] == $00
     
    len := wiz.DeserializeWord(buffer)
    
    'If the length equals 4 we have an IP
    'Otherwise we have text
    <var>if</var>(len == 4)
      buffer += 2 
      <var>bytemove</var>(@@dnsIps[i++], buffer, len)
      buffer += 4
    <var>else</var>
      buffer += (2 + len)
      <var>next</var>
       
  dnsCnt := i    

</pre>
<hr /><h4 id = "lbl30">CreateTransactionId(mask)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl31')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl31><var>PUB</var> CreateTransactionId(mask) 
  transId := <var>CNT</var>
  ?transId
  transId &amp;= mask

</pre>
<hr /><h4 id = "lbl32">FillTransactionID
</h4>
<br /><a onclick ="javascript:ShowHide('lbl33')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl33><var>PUB</var> FillTransactionID
  <var>word</var>[@msgId] := transId
{

</pre>
<hr /><h4 id = "lbl34">SendReceive(buffer, len)
</h4>
<br /><a onclick ="javascript:ShowHide('lbl35')" href="javascript:;">SOURCE CODE...</a>
<pre class=source id=lbl35><var>PUB</var> SendReceive(buffer, len) | bytesToRead 
  <var>RESULT</var> := @null
  bytesToRead := 0
  sock.Open                                             'Open socket and Send Message
  sock.Send(buffer, len)
  bytesToRead := sock.Available
  <var>if</var>(bytesToRead =&lt; 0 )                                 'Check for a timeout
    bytesToRead~
  <var>else</var>  
    <var>RESULT</var> := sock.Receive(buffer, bytesToRead)         'Get the Rx buffer
  sock.Disconnect
} 

</pre>
<hr /><br />
<h2 id = "lbl36">Documentation </h2>
<pre>
This .spin file supports PhiPi's great Spin Code Documenter found at
http://www.phipi.com/spin2html/

You can at any time create a .htm Documentation out of the .spin source.

If you change the .spin file you can (re)create the .htm file by uploading your .spin file
to http://www.phipi.com/spin2html/ and then saving the the created .htm page. 
</pre>
<br />
<h2 id = "lbl37">MIT License </h2>
<pre>
 ______________________________________________________________________________________
|                            TERMS OF USE: MIT License                                 |                                                            
|______________________________________________________________________________________|
|Permission is hereby granted, free of charge, to any person obtaining a copy of this  |
|software and associated documentation files (the "Software"), to deal in the Software |
|without restriction, including without limitation the rights to use, copy, modify,    |
|merge, publish, distribute, sublicense, and/or sell copies of the Software, and to    |
|permit persons to whom the Software is furnished to do so, subject to the following   |
|conditions:                                                                           |
|                                                                                      |
|The above copyright notice and this permission notice shall be included in all copies |
|or substantial portions of the Software.                                              |
|                                                                                      |
|THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,   |
|INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A         |
|PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    |
|HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF  |
|CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE  |
|OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                                         |
|______________________________________________________________________________________|
</pre>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
		</div>
	</td></tr>
</table>
</div>
</body>
</html>	
